﻿using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System.Text;

using Model;
namespace DAL
{
    #region class SQLDALUserInfo
    /// <summary>
    /// it is suitable to the table with only one primary key,and can not deal with the aotu-increment key.
    /// Note:the first field in the table must be primary and autoincrement and int ,if exists 
    /// class:DAL UserInfo 
    /// Author:wxc
    /// DateTime:2021/5/19 16:29:38
    /// </summary>	
    public class DALUserInfo
    {
        #region SQL CONST		
        private const string INSERT_SQL = "INSERT INTO [UserInfo] ([ID],[Pwd],[Type]) Values (@ID,@Pwd,@Type)";
        private const string UPDATE_SQL = "UPDATE [UserInfo] SET Pwd=@Pwd,Type=@Type WHERE [ID]=@ID";
        private const string DEL_SQL = "DELETE FROM [UserInfo] WHERE [ID]=@ID";
        private const string SELECT_ALL_SQL = "SELECT [ID],[Pwd],[Type] FROM [UserInfo] order by iD desc";
        private const string SELECT_ONE_SQL = "SELECT [ID],[Pwd],[Type] FROM [UserInfo] WHERE [ID]=@ID";
        private const string SELECT_WHERE_SQL = "SELECT [ID],[Pwd],[Type] FROM [UserInfo] {$where} order by  iD desc";
        #endregion
        #region Paras const
        private const string PARA_ID = "@ID";
        private const string PARA_Pwd = "@Pwd";
        private const string PARA_Type = "@Type";
        #endregion
        #region Add Method
        /// <summary>
        /// Insert an item
        /// </summary>
        /// <param name="UserInfo"></param>
        /// <returns>count of rows affected</returns>
        public int AddUserInfo(UserInfoEntity UserInfo)
        {
            int backvalue = 0;
            //get paras
            SqlParameter[] UserInfoParms = GetUserInfoParametersInsert();
            //set values to paras
            UserInfoParms[0].Value = UserInfo.ID;
            UserInfoParms[1].Value = UserInfo.Pwd;
            UserInfoParms[2].Value = UserInfo.Type;
            //insert			
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, INSERT_SQL, UserInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Mod Method
        /// <summary>
        /// Update an item
        /// </summary>
        /// <param name="UserInfo"></param>
        /// <returns>count of rows affected</returns>
        public int ModUserInfo(UserInfoEntity UserInfo)
        {
            int backvalue = 0;
            //get paras
            SqlParameter[] UserInfoParms = GetUserInfoParameters();
            //set values to paras
            UserInfoParms[0].Value = UserInfo.ID;
            UserInfoParms[1].Value = UserInfo.Pwd;
            UserInfoParms[2].Value = UserInfo.Type;
            //update			
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, UPDATE_SQL, UserInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Del Method
        /// <summary>
        /// Del an item by primary key
        /// </summary>
        /// <param name="iD"></param> 
        /// <returns>count of rows affected</returns>
        public int DelUserInfo(string iD)
        {
            int backvalue = 0;
            //primarykeys
            //get paras
            SqlParameter[] UserInfoParms = GetUserInfoPrimaryKeyParameters();
            //set values to paras
            UserInfoParms[0].Value = iD;
            //delete
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, DEL_SQL, UserInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Select Method
        /// <summary>
        /// Select an item by Primarykey
        /// </summary>
        /// <param name="iD"></param>
        /// <returns>an item</returns>
        public UserInfoEntity GetUserInfo(string iD)
        {
            UserInfoEntity UserInfo = new UserInfoEntity();
            //primarykeys
            //get paras
            SqlParameter[] UserInfoParms = GetUserInfoPrimaryKeyParameters();
            //set values to paras
            UserInfoParms[0].Value = iD;
            //select
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ONE_SQL, UserInfoParms))
            {
                if (reader.Read())
                {
                    if (!reader.IsDBNull(0)) UserInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) UserInfo.Pwd = reader.GetString(1);
                    if (!reader.IsDBNull(2)) UserInfo.Type = reader.GetString(2);
                }
                else
                {
                    UserInfo = null;
                }
            }
            return UserInfo;

        }
        #endregion
        #region Select_All Method
        /// <summary>
        /// Select all items
        /// </summary>
        /// <returns>items</returns>
        public IList<UserInfoEntity> GetUserInfos()
        {
            IList<UserInfoEntity> UserInfos = new List<UserInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ALL_SQL, null))
            {
                while (reader.Read())
                {
                    UserInfoEntity UserInfo = new UserInfoEntity();
                    if (!reader.IsDBNull(0)) UserInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) UserInfo.Pwd = reader.GetString(1);
                    if (!reader.IsDBNull(2)) UserInfo.Type = reader.GetString(2);
                    UserInfos.Add(UserInfo);
                }
            }
            return UserInfos;
        }
        #endregion
        #region private common select by conditions
        /// <summary>
        /// private common select by conditions
        /// </summary>
        /// <returns>IList<UserInfoEntity></returns>
        private IList<UserInfoEntity> GetUserInfosbyQueryString(string SQLstr)
        {
            IList<UserInfoEntity> UserInfos = new List<UserInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SQLstr))
            {
                while (reader.Read())
                {
                    UserInfoEntity UserInfo = new UserInfoEntity();
                    if (!reader.IsDBNull(0)) UserInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) UserInfo.Pwd = reader.GetString(1);
                    if (!reader.IsDBNull(2)) UserInfo.Type = reader.GetString(2);
                    UserInfos.Add(UserInfo);
                }
            }
            return UserInfos;
        }
        #endregion
        #region public common select by conditions ,we can use it by changing the GetUserInfosbyCondition(datatype condition) and sql
        /// <summary>
        /// Select items by Columns
        /// </summary>
        /// <returns>IList<UserInfoEntity></returns>
        public IList<UserInfoEntity> GetUserInfosbyCondition(string sql)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(SELECT_WHERE_SQL);
            sb.Replace("{$where}", "where " + sql);
            return GetUserInfosbyQueryString(sb.ToString());
        }
        #endregion
        #region Select Method Top n
        /// <summary>
        /// Select items by Columns
        /// </summary>
        /// <returns>IList<UserInfoEntity></returns>
        public IList<UserInfoEntity> GetUserInfosTopN(int n)
        {
            IList<UserInfoEntity> UserInfos = new List<UserInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ALL_SQL, null))
            {
                int i = 0;
                while (reader.Read() && i < n)
                {
                    UserInfoEntity UserInfo = new UserInfoEntity();
                    if (!reader.IsDBNull(0)) UserInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) UserInfo.Pwd = reader.GetString(1);
                    if (!reader.IsDBNull(2)) UserInfo.Type = reader.GetString(2);
                    UserInfos.Add(UserInfo);
                    i++;
                }
            }
            return UserInfos;
        }
        #endregion
        #region paras configuration
        /// <summary>
        /// Internal function to get cached parameters:all paras --- suitable to insert ,especially autoincrement
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetUserInfoParametersInsert()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_ALL_UserInfo_Insert");

            if (parms == null)
            {
                parms = new SqlParameter[] {
            new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
            new SqlParameter(PARA_Pwd,SqlDbType.NVarChar) ,
            new SqlParameter(PARA_Type,SqlDbType.NVarChar)          };
                SQLHelper.CacheParameters("SQL_ALL_UserInfo_Insert", parms);
            }
            return parms;
        }
        /// <summary>
        /// Internal function to get cached parameters:all paras --- suitable to update
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetUserInfoParameters()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_ALL_UserInfo_Update");

            if (parms == null)
            {
                parms = new SqlParameter[] {
                    new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
                    new SqlParameter(PARA_Pwd,SqlDbType.NVarChar) ,
                    new SqlParameter(PARA_Type,SqlDbType.NVarChar)                  };
                SQLHelper.CacheParameters("SQL_ALL_UserInfo_Update", parms);
            }
            return parms;
        }
        /// <summary>
        /// Internal function to get cached parameters:primary key paras
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetUserInfoPrimaryKeyParameters()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_PrimaryKey_UserInfo");

            if (parms == null)
            {
                parms = new SqlParameter[] {
                    new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
                    };
                SQLHelper.CacheParameters("SQL_PrimaryKey_UserInfo", parms);
            }
            return parms;
        }
        #endregion
    }
    #endregion
}
